﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.Catalog;
using PowerCreator.LMS.ViewQuery;

public partial class Theme_Index_SingleTypeCourse : PowerCreator.LMS.Web.Base.WebBasePage
{
   protected  Course opCourse = new Course();
    Catalog opCatalog = new Catalog();
    ViewQuery opViewQuery = new ViewQuery();
    CatalogCourse opCatalogCourse = new CatalogCourse();
    PowerCreator.LMS.Web.Project.ProjectResource objProjectResource = new PowerCreator.LMS.Web.Project.ProjectResource();
    PowerCreator.LMS.Web.DL.Project.TOSchoolStage opSchoolStage = new PowerCreator.LMS.Web.DL.Project.TOSchoolStage();
    PowerCreator.LMS.Web.DL.Project.TOXuenian objXueNian = new PowerCreator.LMS.Web.DL.Project.TOXuenian();
    PowerCreator.LMS.Web.Course.UserCourse opUserCourse = new PowerCreator.LMS.Web.Course.UserCourse();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    TOTrainType objtraintype = new TOTrainType();
    VOTOTrainType votraintype = new VOTOTrainType();
    private string url = "UserLogin.aspx";
    private int pagesize = 16;

    private string _SQL_Catalog_Course = "select distinct"
        + " tcc.CatalogcouID,tg.CatalogId,tg.CatalogName,tc.Course_Id,tc.CourseName,tc.CourseMostlyTeacherName,tc.CourseIsCompulsory,tc.CoursePeriod,tc.CourseState ,tc.CourseResourceNum,tc.XueNian,tc.TrainType,tc.TrainFormat"
        + " from TCatalogCourse tcc left join TCatalog tg on tcc.FKCatalogcouCatalogID=tg.CatalogId "
        + " left join TO_Course tc on tcc.FKCatalogcouCourseID=tc.Course_Id ";

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = "课程菜单";
        CheckUser();
        ViewState["TypeID"] = 0;
        string typeid = "0";
        if (Request.QueryString["TypeID"] != null)
        {
            typeid = Request.QueryString["TypeID"].ToString();
            ViewState["TypeID"] = typeid;
        }
     
        if (!IsPostBack)
        {
            BinderCatalog();
            BinderXueduan();
            BinderXueNian();

            BindTrainType();

            votraintype = objtraintype.Load(int.Parse(typeid));
            lbl_typeName.Text = votraintype.TrainTypeName;

        }
        string search = GetSearch(typeid);
        BinderPager(search);
        BinderPage(search, 1);

    }
    protected override void InitPage()
    {
        base.InitPage();
    }


    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectURL(url);
        }

        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login == null)
        {
            Script.RedirectURL(url);
        }
        //else
        //{
        //    验证此课件该用户是否可以点播
        //    PowerCreator.LMS.Web.User.LoginEntity login = (PowerCreator.LMS.Web.User.LoginEntity)Session[WebConfig.SESSION_USER_USERINFO];
        //    string courseIDlist = getMyCourseIDlist();
        //}
    }

    private string GetSearch(string typeid)
    {
        StringBuilder sbSearch = new StringBuilder();
        sbSearch.Append(_SQL_Catalog_Course + " where 1=1 and tc.Used=1 and tc.TrainType like '%," + typeid + ",%'");

        return sbSearch.ToString();

    }
    protected void btn_seach_Click(object sender, EventArgs e)
    {
        string str = "";
        if (ddl_Catalog.SelectedValue != "0")
            str = "catalogid=" + ddl_Catalog.SelectedValue;
        else
            str = "catalogid=0";
        if (ddl_xueduan.SelectedValue != "0")
            str = str + "&xueduan=" + ddl_xueduan.SelectedValue;
        else
            str = str + "&xueduan=0";
        if (ddl_xuenian.SelectedValue != "0")
            str = str + "&xuenian=" + ddl_xuenian.SelectedValue;
        else
            str = str + "&xuenian=0";

        if (!string.IsNullOrEmpty(txt_KeyWord.Text))
            str = str + "&KeyWord=" + txt_KeyWord.Text;
        else
            str = str + "&KeyWord=";
        str = str + "&traintype=" + getTrainTypeIDlist();

        string url = "SearchCourse.aspx?" + str;
        Script.RedirectURL(url);
    }
    private string getTrainTypeIDlist()
    {
        string trainTypeIDlist = "-1,";

        for (int j = 0; j < chk_peixunleixing.Items.Count; j++)
        {
            if (chk_peixunleixing.Items[j].Selected)
            {
                trainTypeIDlist += chk_peixunleixing.Items[j].Value + ",";
            }
        }
        trainTypeIDlist += "0";
        if (trainTypeIDlist == "-1,0")
            trainTypeIDlist = "";
        return trainTypeIDlist;
    }
    private void BinderCatalog()
    {
        string search = "";
        DataTable dt = opCatalog.QueryCatalog(search);
        ArrayList list = opCatalog.GetCatalogTree(dt);
        //opCatalog.GetCatalogTreeView(dt,ref tv_Catalog);

        ddl_Catalog.DataSource = list;
        ddl_Catalog.DataTextField = "Text";
        ddl_Catalog.DataValueField = "Value";
        ddl_Catalog.DataBind();

        ListItem li = new ListItem();
        li.Text = "全部学科";
        li.Value = "0";
        ddl_Catalog.Items.Insert(0, li);
    }
    private void BinderXueduan()
    {
        string search = "";
        DataTable dt = opSchoolStage.Query(search);

        ddl_xueduan.DataSource = dt;
        ddl_xueduan.DataTextField = "SchoolStage";
        ddl_xueduan.DataValueField = "ID";
        ddl_xueduan.DataBind();

        ListItem li = new ListItem();
        li.Text = "全部学段";
        li.Value = "0";
        ddl_xueduan.Items.Insert(0, li);

    }
    private void BinderXueNian()
    { 
        string search = "";
        DataTable dt = objXueNian.Query(search);

        ddl_xuenian.DataSource = dt;
        ddl_xuenian.DataTextField = "XueNian";
        ddl_xuenian.DataValueField = "ID";
        ddl_xuenian.DataBind();

        ListItem li = new ListItem();
        li.Text = "全部学年";
        li.Value = "0";
        ddl_xuenian.Items.Insert(0, li);

    }
    /// <summary>
    /// 绑定培训类型
    /// </summary>
    private void BindTrainType()
    {
        string search = " IsShow=1";
        DataTable dt = objtraintype.Query(search);

        chk_peixunleixing.DataSource = dt;
        chk_peixunleixing.DataTextField = "ShortName";
        chk_peixunleixing.DataValueField = "ID";
        chk_peixunleixing.DataBind();
    }

    private void BinderPage(string search, int pageIndex)
    {
            DataTable dt01 = new DataTable();
            dt01 = opViewQuery.Query(search, pagesize, pageIndex);
            dt01.Columns.Add("CourseIsCompulsoryStr");
            dt01.Columns.Add("TrainFormatStr");
            for (int j = 0; j < dt01.Rows.Count; j++)
            {
                if (dt01.Rows[j]["CourseIsCompulsory"].ToString() == "1")
                    dt01.Rows[j]["CourseIsCompulsoryStr"] = "必修";
                else
                    dt01.Rows[j]["CourseIsCompulsoryStr"] = "选修";
                if (dt01.Rows[j]["TrainFormat"].ToString() == "1")
                    dt01.Rows[j]["TrainFormatStr"] = "网络";
                else if (dt01.Rows[j]["TrainFormat"].ToString() == "2")
                    dt01.Rows[j]["TrainFormatStr"] = "面授";
                else
                    dt01.Rows[j]["TrainFormatStr"] = "面授加网络";
            }
            rpt_Course.DataSource = dt01;
            rpt_Course.DataBind();

            for (int k = 0; k < rpt_Course.Items.Count; k++)
            {
                Label lbl_CourseIsCompulsory = (Label)rpt_Course.Items[k].FindControl("lbl_CourseIsCompulsory");
                Label lbl_CourseId = (Label)rpt_Course.Items[k].FindControl("lbl_CourseId");
                Button btn_sel = (Button)rpt_Course.Items[k].FindControl("btn_sel");
                if (lbl_CourseIsCompulsory.Text == "1")
                    btn_sel.Enabled = false;
                else
                {
                    string sql = "select UCID from UserCourse where FKUCCourseID=" + lbl_CourseId.Text + " and UCUserID=" + login.UserID.ToString() + "";
                    int count = opViewQuery.Count(sql);
                    if(count==1)
                        btn_sel.Enabled = false;
                }
                if (btn_sel.Enabled == true)
                {
                    btn_sel.Attributes.Add("onclick", "selcourse(" + btn_sel.CommandArgument.ToString() + ");");
                }
            }
    }


    private void BinderPager(string search)
    {
        AspNetPager1.PageSize = pagesize;
        AspNetPager1.RecordCount = opViewQuery.Count(search);
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch(ViewState["TypeID"].ToString());
        BinderPage(search, AspNetPager1.CurrentPageIndex);
    }  

    

    //protected void rptCourse_ItemCommand(object source, RepeaterCommandEventArgs e)
    //{
    //    if (e.CommandName.ToLower() == "sel")
    //    {
    //        string search = string.Format(" UCUserID={0} and FKUCCourseID = {1} ", login.UserID, e.CommandArgument.ToString());
    //        if (opUserCourse.QueryUserCourse(search) == 0)
    //        {
    //            PowerCreator.LMS.Course.Entity.UserCourse voUserCourse = new PowerCreator.LMS.Course.Entity.UserCourse();
    //            voUserCourse.FKUCCourseID = int.Parse(e.CommandArgument.ToString());
    //            voUserCourse.UCState = 1;
    //            voUserCourse.UCUserID = login.UserID;
    //            opUserCourse.AddUserCourse(voUserCourse);

    //            Script.Alert("选课成功，请等待管理员审核！");
    //        }
    //    }
    //}
}
